#ifndef _MAILS_H_
#define _MAILS_H_


#include "Common.h"
#include "String.h"
#include "SharedPtr.h"
#include "Resource.h"
#include "Vector3.h"
#include "EffectModels.h"

class CreatureAIScript;
class ItemPtr;
class EffectPtr;
//邮件公共属性
typedef struct mail_common_property
{
	ResourceProxy		uplink;
	void initialize(void)
	{
		uplink.setNull();
	}

	mail_common_property()
	{
		initialize();
	}

}mail_common_property_t;

class Mail :public Resource
{
public:
	enum MailStatus
	{
		MailStatusNone			= 0,	//无效状态
		MailStatusDraft			= 1,	//草稿状态
		MailStatusSending		= 2,	//邮件发送中
		MailStatusRecv			= 3,	//邮件到达
		MailStatusOpen			= 4,	//邮件打开
		MailStatusClose			= 5,	//邮件结束
		MailStatusError			= 7,	//处理错误
		MailStatusDeleted		= 255,	//邮件删除
	};

	enum MailFlag
	{
		MailFlagNormal			= 0x01,		//一般邮件
		MailFlagPayment			= 0x02,		//付费邮件
		MailFlagTrans			= 0x04,		//转账邮件
		MailFlagReadNotice		= 0x08,		//看信通知
		MailFlagCloseNotice		= 0x10,		//信件确认通知
		MailFlagSendRemove		= 0x20,		//邮件发送方删除
		MailFlagRecvRemove		= 0x40		//邮件接收方删除
	};

	enum MailPayStatus
	{
		MailPayStatusNone		= 0,		//免费邮件
		MailPayStatusWait		= 1,		//等待支付
		MailPayStatusAccept		= 2,		//确认支付
		MailPayStatusDeny		= 3		//拒绝支付
	};

	Mail(ResourceManager* creator, const String& name, ResourceHandle handle,
            const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
	~Mail(void);

protected:
       	void prepareImpl(void);
        /** Destroys data cached by prepareImpl.
         */
        void unprepareImpl(void);
        /// @copydoc Resource::loadImpl
        bool loadImpl(void);
        /// @copydoc Resource::postLoadImpl
        void postLoadImpl(void);

	 void preUnloadImpl(void);
        /// @copydoc Resource::unloadImpl
        void unloadImpl(void);
        /// @copydoc Resource::calculateSize
        size_t calculateSize(void) const;

	bool loadChildrenFromDB(void);

private:
	uint32		m_serial;
	uint32		m_group_id;

	std::list<ResourceProxy>        m_items;        // 装备的物品

	void    initialize(void);
	size_t loadMailItemsFromDB(void);
public:

	const uint32  & getSerial() 	{	return m_serial;	}
	const uint32  & getGroupID()	{	return m_group_id;	}

	bool hasChildren(void)  {       return true;    }

	//添加释放容器物品包含的物品
	bool	addItem(const uint32 & serial);
	bool  	addItem(ResourceProxy & item);
	bool	removeItem(const uint32 & serial);
	bool	removeItem(ResourceProxy & item);
	bool    deleteItem(const uint32 & serial);
        bool    deleteItem(ResourceProxy & item);
        bool    destroyItem(const uint32 & serial);
        bool    destroyItem(ResourceProxy & item);
	void	removeAllItems(void);
	void	destroyAllItems(void);
	void	deleteAllItems(void);

	//save item to database	
	bool	SaveDB(void);
	bool	LoadDB(void);
	bool    FetchDB(Field * fields);

	void 	Delete(void);
	void	Destroy(void);

	MailStatus getMailStatus(void);
	void	setMailStatus(const MailStatus & status);


	//得到包含物品的列表复制
        std::list<ItemPtr> * getItemsList(std::list<ItemPtr> * list);

	//公共属性
	mail_common_property_t	common;

	
};

    /** Specialisation of SharedPtr to allow SharedPtr to be assigned to MailPtr 
    @note Has to be a subclass since we need operator=.
    We could templatise this instead of repeating per Resource subclass, 
    except to do so requires a form VC6 does not support i.e.
    ResourceSubclassPtr<T> : public SharedPtr<T>
    */
    class SERVER_DECL  MailPtr : public SharedPtr<Mail>
    {
    public:
        MailPtr() : SharedPtr<Mail>() {}
        explicit MailPtr(Mail* rep) : SharedPtr<Mail>(rep) {}
        MailPtr(const MailPtr& r) : SharedPtr<Mail>(r) {}
        MailPtr(const ResourcePtr& r);
        /// Operator used to convert a ResourcePtr to a MeshPtr
        MailPtr& operator=(const ResourcePtr& r);
    protected:
        /// Override destroy since we need to delete Mesh after fully defined
        void destroy(void);
    };
	

#endif
